package com.bosssoft.controller;

import com.bosssoft.permission.centre.service.TokenService;
import com.sd365.common.core.common.pojo.vo.TenantBaseVO;
import com.bosssoft.permission.centre.entity.Role;
import com.bosssoft.permission.centre.entity.User;
import com.bosssoft.permission.centre.pojo.dto.pcp.CompanyDTO;
import com.bosssoft.permission.centre.pojo.vo.ResourceVO;
import com.bosssoft.permission.centre.service.CompanyService;
import com.bosssoft.permission.centre.service.LoginService;
import com.sd365.common.util.TokenUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;


@RestController
@RequestMapping(value = "/permission/centre")
public class LoginController {


    @Autowired
    LoginService loginService;
    @Autowired
    CompanyService companyService;
    @Autowired
    TokenService tokenService;
    @Data
    class ResponseData {
        private String token;
        private List<String> roles;
        private String name;
        private String avatar;
        private List<ResourceVO> resourceVO;


    }

    @Data
    public class UserVO {
        private String name;
        private String tel;
        private Long id;
        private String msg;
        private int code;
        private Long tenantId;
        private String companyAddress;
        private Long orgId;
        private Long companyId;
        private ResponseData data;
        private List<Long> roleIds;


    }

    @CrossOrigin
    @GetMapping(value = "/vue-admin-template/user/auth")
    @ResponseBody
    public UserVO auth(@RequestParam(value="code") String code, @RequestParam(value="password") String password, @RequestParam(value="account") String account){
        UserVO userVO = new UserVO();

        int success = loginService.verification(code, DigestUtils.md5DigestAsHex(password.getBytes()), account);
        if(success == 1) {
            User user = loginService.getUserInfo(code, account);
            if (user == null) {
                userVO.setCode(401);
                userVO.setMsg("账号和租户不匹配或不存在");
                return userVO;
            }
            List<Long> roleIds = new LinkedList<>();
            List<Role> roles = loginService.getRolesByUserId(user.getId());
            for (Role role : roles) {
                roleIds.add(role.getId());
            }
            ResponseData responseData = new ResponseData();
            responseData.setToken("");
            userVO.setData(responseData);
            userVO.setRoleIds(roleIds);
            userVO.setTenantId(user.getTenantId());
            userVO.setOrgId(user.getOrgId());
            userVO.setCompanyId(user.getCompanyId());
            userVO.setCode(200);
            userVO.setMsg("登录成功");
            User userInfo = loginService.getUserInfo(code, account);
            userVO.setId(userInfo.getId());
            userVO.setName(user.getName());
            userVO.setTel(user.getTel());
        }else if(success == 0) {
            userVO.setCode(401);
            userVO.setMsg("账号和租户不匹配或不存在");
        }else {
            userVO.setCode(401);
            userVO.setMsg("用户名或密码错误，登录失败");
        }
        return userVO;
    }



    @CrossOrigin
    @GetMapping(value = "/vue-admin-template/user/login")
    @ResponseBody
    public UserVO login(@RequestParam(value="code") String code, @RequestParam(value="password") String password, @RequestParam(value="account") String account) {
        // 根据user.code, user.password, tenant.account
        UserVO userVO = new UserVO();
        userVO.data = new ResponseData();
        System.out.println("1111");
        int flag = loginService.verification(code, DigestUtils.md5DigestAsHex(password.getBytes()), account);
        System.out.println("2222");
        if(flag == 1) {

            userVO.data.setToken("1");
            userVO.setCode(200);
            userVO.setMsg("登录成功");
            User userInfo = loginService.getUserInfo(code, account);
            userVO.setId(userInfo.getId());
            userVO.setCompanyId(userInfo.getCompanyId());
            userVO.setTenantId(userInfo.getTenantId());
            userVO.setOrgId(userInfo.getOrgId());

            List<Long> roleIds = new LinkedList<Long>();
            List<Role> roles = loginService.getRolesByUserId(userInfo.getId());
            for (Role role : roles) {
                roleIds.add(role.getId());
            }
            String token = tokenService.getToken(userInfo,userInfo, account, roleIds);
            userVO.data.setToken(TokenUtil.encoderToken(token));
        }else if(flag == 0) {
            userVO.setCode(401);
            userVO.setMsg("账号和租户不匹配或不存在");
        }else {
            userVO.setCode(401);
            userVO.setMsg("用户名或密码错误，登录失败");
        }
        return userVO;
    }


    @CrossOrigin
    @GetMapping(value = "/vue-admin-template/user/info")
    @ResponseBody

    public UserVO info(@RequestParam(value="code") String code, @RequestParam(value="account") String account) {

        // 查找对应的用户
        User user = loginService.getUserInfo(code, account);
        // 查找用户对应的角色
        List<Role> roles = loginService.getRolesByUserId(user.getId());
        List<String> rolesName = new LinkedList<>();
        for(Role role : roles) {
            rolesName.add(role.getName());
        }
        // 查找对应的资源ResourceVO
        List<ResourceVO> resourceVO = loginService.getResourceVO(roles);

        UserVO userVO = new UserVO();
        userVO.data = new ResponseData();
        userVO.data.setAvatar(user.getProfilePicture());
        userVO.data.setRoles(rolesName);
        userVO.data.setName(user.getName());
        userVO.data.setResourceVO(resourceVO);
        userVO.setCode(200);
        userVO.setTel(user.getTel());
        userVO.setTenantId(user.getTenantId());
        userVO.setCompanyId(user.getCompanyId());
        userVO.setOrgId(user.getOrgId());
        userVO.setId(user.getId());
        userVO.setMsg("登录成功");
        User userInfo = loginService.getUserInfo(code, account);
        userVO.setId(userInfo.getId());
        CompanyDTO companyDTO = companyService.queryById(user.getCompanyId());
        userVO.setCompanyAddress(companyDTO==null?companyDTO.getAddress():"福州宇宏物流园");
        System.out.println("##resourceVO:" + resourceVO);
        System.out.println(userVO);
        return userVO;

    }


    @CrossOrigin
    @GetMapping(value = "/vue-admin-template/user/logout")
    @ResponseBody
    public UserVO logout(){
        UserVO userVO = new UserVO();
        userVO.data = new ResponseData();
        System.out.println("logout.................");
        return userVO;
    }
}
